package cn.yitun.chat.yitunserverimapicore.business.contact.service.impl;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.Info;
import cn.yitun.chat.yituncommonutil.YitunCollectionUtil;
import cn.yitun.chat.yituncommonutil.YitunStringUtil;
import cn.yitun.chat.yitunservercommonlib.crud.yitun.CrudServiceImpl;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.contact.ContactAddVerifyQuestionDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.contact.ContactHarassSettingDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.po.contact.ContactHarassSettingInfo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.contact.ContactAddVerifyQuestionVo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.contact.ContactHarassSettingVo;
import cn.yitun.chat.yitunservercommonlib.enums.groupChat.GroupJoinVerifySettingTypeEnums;
import cn.yitun.chat.yitunservercommonlib.util.NumUtil;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunserverimapicore.business.contact.service.HarassSettingService;
import cn.yitun.chat.yitunserverimbase.dao.contact.ContactAddVerifyQuestionDao;
import cn.yitun.chat.yitunserverimbase.dao.contact.ContactHarassSettingDao;
import cn.yitun.chat.yitunserverimcore.general.main.biz.contact.manager.ContactHarassSettingManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Date 2019-01-20 18:21:27<br>
 * Description
 *
 * @author yitun<br>
 * @version 1.1
 */
@Service
@Transactional
public class HarassSettingServiceImpl
		extends CrudServiceImpl<ContactHarassSettingDao, ContactHarassSettingDto, ContactHarassSettingInfo, ContactHarassSettingVo>
		implements HarassSettingService {

	@Resource
	private ContactAddVerifyQuestionDao contactAddVerifyQuestionDAO;
	@Resource
	private ContactHarassSettingManager contactHarassSettingManager;

	/**
	 * 
	 * Date 2019-01-20 18:59:56<br>
	 * Description 获取用户防骚扰设置
	 * 
	 * @param userId
	 * @return
	 * @author yitun<br>
	 * @version 1.1
	 */
	public ContactHarassSettingVo getByUserId(Long userId) {
		ContactHarassSettingVo data = contactHarassSettingManager.getByUserId(userId);
		return data;
	}

	/**
	 * 
	 * Date 2019-01-20 19:01:54<br>
	 * Description 获取用户验证问题列表
	 * 
	 * @param userId
	 * @return
	 * @author yitun<br>
	 * @version 1.1
	 */
	public List<ContactAddVerifyQuestionVo> getVerifyQuestionListByUserId(Long userId) {
		List<ContactAddVerifyQuestionVo> list = contactAddVerifyQuestionDAO.listVO(
				R.param("userId", userId));
		return list;
	}

	@Override
	public Info saveWithQuestiones(ContactHarassSettingDto data) {
		List<ContactAddVerifyQuestionDto> list = data.getQuestionList();
		Info info = new Info();
		try {
			if (GroupJoinVerifySettingTypeEnums.VERIFY_TYPE_ANSWER.getType().equals(data.getVerifyType())) {
				if (YitunStringUtil.isEmpty(data.getQuestion()))
					info.addWarning(ExceptionEnum.SOCK_CONTACT_QUESTION_CANT_BE_NULL);

				if (YitunStringUtil.isEmpty(data.getAnswer()))
					info.addWarning(ExceptionEnum.SOCK_CONTACT_ANSWER_CANT_BE_NULL);

			}
			if (GroupJoinVerifySettingTypeEnums.VERIFY_TYPE_CONFIRM.getType().equals(data.getVerifyType())) {
				if (YitunCollectionUtil.isEmpty(list)) {
					info.addWarning(ExceptionEnum.SOCK_CONTACT_QUESTION_CANT_BE_NULL);
				} else {
					int i = 1;
					for (ContactAddVerifyQuestionDto d : list) {
						if (YitunStringUtil.isEmpty(d.getQuestion())) {
							info.addWarning(ExceptionEnum.SOCK_CONTACT_QUESTION_CANT_BE_NULL.getCode().toString(), "第" + i + "问题不能为空！");
						}
						i++;
					}
				}
			}
			if (info.isSuccess()) {
				saveOrUpdate(data);
				saveOrUpdate(data.getUserId(), list);
			}
		} catch (Exception e) {
			info.addWarning(ExceptionEnum.BUSY_SERVER);
		}
		return info;
	}

	/**
	 * 保存
	 * @param data
	 */
	public Info saveOrUpdate(ContactHarassSettingDto data) {

		if (YitunCollectionUtil.isEmpty(data.getQuestionList())) {
			return saveWithQuestiones(data);
		} else {
			Long userId = data.getUserId();
			Long id = dao.getIdByUserId(userId);
			if (NumUtil.isInValid(id)) {
				dao.save(data);
			} else {
				data.setId(id);
				dao.update(data);
			}
			return null;
		}
	}

	/**
	 * 先删除后保存
	 * @param userId
	 * @param list
	 */
	public void saveOrUpdate(Long userId, List<ContactAddVerifyQuestionDto> list) {
		contactAddVerifyQuestionDAO.deleteByUserId(userId);
		for (ContactAddVerifyQuestionDto data : list) {
			data.setUserId(userId);
			contactAddVerifyQuestionDAO.save(data);
		}
	}
}
